<script>
import UploadExcel from '@/components/uploadExcel/uploadExcel.vue'
import { transitionExcelHeaderToEnKey } from '@/utils'

export default {
  name: 'uploadDialog',
  components: { UploadExcel },
  props: {
    showUploadDialog: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    /* 对话框关闭时的回调函数 */
    handleClose() {
      this.$emit('update:showUploadDialog', false)
    },
    /* 文件上传之前的回调函数 */
    beforeUpload(file) {
      const isLt1M = file.size / 1024 / 1024 < 1
      if (isLt1M) return true
      this.$message.warning('请不要上传大于1m的文件')
      return false
    },
    /* 文件上传成功的回调函数 */
    async handleSuccess({ results, header }) {
      try {
        const data = transitionExcelHeaderToEnKey(results)
        const message = await this.$store.dispatch('employeesStore/importExcel', data)
        this.$message.success(message)
        this.$emit('submit')
      } catch (error) {
        this.$message.error(error.message)
      }
    }
  },
  emits: ['submit']
}
</script>

<template>
  <el-dialog title="导入excel" :visible="showUploadDialog" @close="handleClose">
    <upload-excel :on-success="handleSuccess" :before-upload="beforeUpload"></upload-excel>
  </el-dialog>
</template>

<style scoped lang="scss">

</style>
